<?php
require_once '../libs/app.config.php';
session_start();
if($_SESSION['LoginId'] == false){
	header("Location:login.php");
}

$id=$_GET['id'];
//添加无限分类
	if($id==""){
//无限分类下拉列表
function mainfl()
{ global $show;
	$result=mysql_query("select id,partent_id,cat_name from article_cat where partent_id=0 and( cat_name='CARGO SERVICES' or cat_name = 'EXPRESS SERVICES')  order by id");
	if ($myrow=mysql_fetch_array($result))
	{
	  do{
		 $show.="<option value=".$myrow['id'].">".$myrow['cat_name']."</option>";
		 subfl($myrow["id"],"&nbsp;");
		}
	  while ($myrow=mysql_fetch_array($result));
	}
}
function subfl($fid,$tag)
{   global $show;
	$result1=mysql_query("select id,partent_id,cat_name from article_cat where partent_id=$fid  order by id");
	if ($myrow1=mysql_fetch_array($result1))
	{
		do {
			$show.="<option value=".$myrow1['id'].">".$tag.$myrow1["cat_name"]."</option>";
			subfl($myrow1["id"],"&nbsp;".$tag);
			}
		while ($myrow1=mysql_fetch_array($result1));
	}
}

mainfl();

	}

	if($_GET['act']=='add')
	{
		//添加无限分类
		if($_POST['submit'] && $_POST['id']=="")
	{
		$query   = $tpm->insert("article_cat",array(
		   "cat_name"  => $_POST['title'],
		   "cat_name_en"  => $_POST['title2'],
		   "cat_name_cn"  => $_POST['title3'],
		   "partent_id"  => $_POST['partent_class'],
	  ));
	  $insert_id = $tpm->insert_id();
	  $sql =sprintf('select acp.level from article_cat acp, article_cat acs where acs.id=%d and acs.partent_id=acp.id',$insert_id);
	  $query = $tpm->query($sql);
	  while($row=$tpm->fetch_array($query)){
	  	$level = $row['level'];
	  }
	  isset($level)?null:$level=-1;

	  $tpm->update("article_cat",array('sort_order'=>$insert_id,'level'=>$level+1),"id=$insert_id");
		 header("Location:news_catlist.php");
	}

	}
	//修改分类
	if($id!="")
	{
	//无限分类下拉列表
function mainfl()
{ global $show;
	$result=mysql_query("select id,partent_id,cat_name from article_cat where partent_id=0 order by id");
	if ($myrow=mysql_fetch_array($result))
	{
	  do{
		 $show.="<option value=".$myrow['id'].">".$myrow['cat_name']."</option>";
		 subfl($myrow["id"],"&nbsp;");
		}
	  while ($myrow=mysql_fetch_array($result));
	}
}
function subfl($fid,$tag)
{   global $show;
	$result1=mysql_query("select id,partent_id,cat_name from article_cat where partent_id=$fid  order by id");
	if ($myrow1=mysql_fetch_array($result1))
	{
		do {
			$show.="<option value=".$myrow1['id'].">".$tag.$myrow1["cat_name"]."</option>";
			subfl($myrow1["id"],"&nbsp;".$tag);
			}
		while ($myrow1=mysql_fetch_array($result1));
	}
}

mainfl();

	$sqlq="select cat_name,cat_name_en,cat_name_cn from article_cat where id='".$_GET['id']."'";
	$query=$tpm->query($sqlq);
	$rowq=$tpm->fetch_array($query);
	$cate_name=$rowq['cat_name'];
    $cate_name_en=$rowq['cat_name_en'];
    $cate_name_cn=$rowq['cat_name_cn'];

	$sql="select partent_id from article_cat where id='".$_GET['id']."'";
	$query=$tpm->query($sql);
	$row=$tpm->fetch_array($query);
    $cate_id=$row['partent_id'];

	$sqls="select * from article_cat where id='".$cate_id."'";
	$querys=$tpm->query($sqls);
	$rows=$tpm->fetch_array($querys);
    $cate_title=$rows['cat_name'];
	$catep_id=$rows['id'];

	}


	if($_GET['act']=='edit')
	{
		//修改无限分类

			if($_POST['submit'] && $_POST['id']!="")
	{
	  $sql =sprintf('select acp.level from article_cat acp where %d=acp.id',$_POST['partent_class']);
	  $query = $tpm->query($sql);
	  while($row=$tpm->fetch_array($query)){
	  	$level = $row['level'];
	  }
	  isset($level)?null:$level=-1;
	  $level++;
	     $sql="update article_cat set cat_name='".$_POST['title']."',cat_name_en='".$_POST['title2']."',cat_name_cn='".$_POST['title3']."',partent_id='".$_POST['partent_class']."',level=$level where id='".$_POST['id']."'";
		 $query=$tpm->query($sql);
		 header("Location:news_catlist.php");
	}
	}
require_once(INCLUDE_PATH.'template.inc.php');  //模板类及对象$tpl的加载
    $tpl->assign("cate_title",$cate_title);
    $tpl->assign("cate_id",$cate_id);
	$tpl->assign("cate_name",$cate_name);
    $tpl->assign("cate_name_en",$cate_name_en);
    $tpl->assign("cate_name_cn",$cate_name_cn);
	$tpl->assign("id",$id);
    $tpl->assign("show",$show);
	$tpl->assign("catelist",$catelist);
	$tpl->display("news_catadd.html");
?>